﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Security.Claims;
using Traceability.APP.Application.Command.Traceablitys;
using Traceability.APP.DTO;
using Traceability.Damoin;
using Traceability.ErrorCode;

namespace Traceability.APP.Controllers
{
 /// <summary>
 /// 溯源
 /// </summary>

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MaxageController : ControllerBase
    {
      private readonly  IMediator mediator;

        public MaxageController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public Task<APIResult<Account>> Login([FromQuery] LoginCommand login)
        {
            try
            {

                var result = mediator.Send(login);
                return result;
            }
            catch (Exception ex)
            {
                
                throw;
            }

        }
        /// <summary>
        /// 获取溯源表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<APIPaing<List<TraceablityDto>>>> GetAccount([FromQuery] GetTraceablityCommand command)
        {
            try
            {
                var result = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {

                throw;
            }
        }
        /// <summary>
         /// 获取商品表
         /// </summary>
         /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<Goods>>> GetGoods()
        {
            try
            {
                GetGoodsCommand command = new GetGoodsCommand();
                var result = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {

                throw;
            }
        }
        /// <summary>
        /// 溯源新增
        /// </summary>
        /// <param name="request">实体</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddTraceablity(AddTraceablityCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 溯源修改
        /// </summary>
        /// <param name="request">实体</param>
        /// <returns>返回任务</returns>
        [HttpPut]
        public async Task<APIResult<int>> UpdateTraceablity(UpdateTraceablityCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 删除溯源
        /// </summary>
        [HttpPost]
        public async Task<APIResult<int>> BatchDeleteTraceablity([FromBody] BatchDeleteTraceabilyCommand command)
        {
            return await mediator.Send(command);
        }
    }
}
